# integer —— 整数,string —— 字符串,object —— 对象,array —— 数组,number —— 整数/⼩数,null —— 空值,boolean —— 布尔值
# 语法：{"type": "数据类型" }
import jsonschema

schema = {
    "type": "object",
    "properties": {
        "success": {"type": "boolean"},
        "code": {"type": "integer"},
        "message": {"type": "string"},
        "money": {"type": "number"},
        "address": {"type": "null"},
        "data": {"type": "object"},
        "luckyNumber": {"type": "array"},
    }
}

data = {
    "success": True,
    "code": 10000,
    "message": "操作成功",
    "money": 6.66,
    "address": None,
    "data": {
        "name": "tom"
    },
    "luckyNumber": [6, 8, 9]
}

res = jsonschema.validate(instance=data, schema=schema)
print(res)

schema = {
    "type": "object",
    "properties": {
        "success": {"type": "boolean"},
        "code": {"type": "integer"},
        "message": {"type": "string"},
        "money": {"type": "number"},
        "address": {"type": "null"},
        "data": {"type": "object",
                 "properties": {
                     "name": {"type": "string"}
                 }
                 },
        "luckyNumber": {"type": "array"},
    }
}

data = {
    "success": True,
    "code": 10000,
    "message": "操作成功",
    "money": 6.66,
    "address": None,
    "data": {
        "name": "tom"
    },
    "luckyNumber": [6, 8, 9]
}

res = jsonschema.validate(instance=data, schema=schema)
print(res)

schema = {"type": "object",
          "required": ["success", "code", "message"]
          }
data = {
    "success": True,
    "code": 10000,
    "message": "操作成功",
    "data": None
}

res = jsonschema.validate(instance=data, schema=schema)
print(res)

data = {
    "success": True,
    "code": 10000,
    "message": "操作成功",
    "data": None
}

schema = {
    "type": "object",
    "properties": {
        "success": {"const": True},
        "code": {"const": 10000},
        "message": {"const": "操作成功"},
        "data": {"const": None}
    }
}
res = jsonschema.validate(instance=data, schema=schema)
print(res)

data = {
    "message": "操作成功!",
    "mobile": "13800000002"
}
schema = {
    "type": "object",
    "properties": {"message": {"pattern": "操作成功"},
                   "mobile": {"pattern": "^[0-9]{11}$"
                              }

                   }
}
print(jsonschema.validate(instance=data, schema=schema))
